package com.calligraphy.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.calligraphy.bases.utils.SetMsg;
import com.calligraphy.order.converter.OrderForm2OrderDTOConverter;
import com.calligraphy.order.dto.OrderDTO;
import com.calligraphy.order.enums.ResultEnum;
import com.calligraphy.order.exception.OrderException;
import com.calligraphy.order.from.OrderForm;
import com.calligraphy.order.service.BuyerService;
import com.calligraphy.order.service.OrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 买家端订单
 * @Author hy
 * @Date 2019/1/11 13:32
 **/
@Slf4j
@RestController
@RequestMapping("/buyer/order")
//@CacheConfig(cacheNames = "order")
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    @ApiOperation(value="创建订单", notes="根据url的orderForm来创建订单")
    @ApiImplicitParam(name = "orderForm", value = "买家信息列表", required = true, dataType = "OrderForm")
    @RequestMapping("/create")
    //@CachePut(cacheNames = "list",key = "123") //每次都會執行更新redis的数据
    @CacheEvict(cacheNames = "list",key = "123")    //更新会清除redis
    public JSONObject create(@Valid OrderForm orderForm,
                             BindingResult bindingResult){
        JSONObject json = new JSONObject();
        if (bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new OrderException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车信息为空");
            throw new OrderException(ResultEnum.CART_EMPTY);
        }

        OrderDTO result = orderService.create(orderDTO);
        json.put("orderId", result.getOrderId());
        SetMsg.setMessage(json, 0,"success");
        return json;
    }
    //condition :缓存条件
    @PostMapping("/list")
    @CachePut(cacheNames = "list",key = "#buyerId",condition = "#buyerId.length() > 3",unless = "#json.get('resCode') != 0")
    public JSONObject list(@RequestParam("buyerId") String buyerId,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size){
        JSONObject json = new JSONObject();
        if(StringUtils.isEmpty(buyerId)){
            log.error("【查询订单列表】buyerId为空");
            throw new OrderException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request=PageRequest.of(page,size);
        Page<OrderDTO> orderDTOPage=orderService.findList(buyerId,request);
        json.put("orderContent",orderDTOPage.getContent());
        SetMsg.setMessage(json, 0,"success");
        return json;
    }
    /**
     * 订单详情
     * @param buyerId
     * @param orderId
     */
    @PostMapping("/detail")
    public JSONObject detail(@RequestParam("buyerId") String buyerId,
                                     @RequestParam("orderId") String orderId){
        JSONObject json = new JSONObject();
        OrderDTO orderDTO=buyerService.findOrderOne(buyerId,orderId);
        json.put("orderDTO",orderDTO);
        SetMsg.setMessage(json, 0,"success");
        return json;
    }

    /**
     * 取消订单
     * @param buyerId
     * @param orderId
     */
    @PostMapping("/cancel")
    public JSONObject cancel(@RequestParam("buyerId") String buyerId,
                           @RequestParam("orderId") String orderId) {
        JSONObject json = new JSONObject();
        buyerService.cancelOrder(buyerId, orderId);
        SetMsg.setMessage(json, 0, "success");
        return json;
    }
}
